package cn.web.web_module.service.impl;

import cn.itcast.mapper.orderMapper;
import cn.itcast.pojo.order;
import cn.itcast.util.ResponseEntity;
import cn.web.web_module.service.OrderAddressFindService;
import cn.web.web_module.util.MyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: 张松
 * @Description:
 * @Date: Created in 17:30 2020/8/4
 * @Modified By:
 */
@Service("OrderAddressFind")
public class OrderAddressFindServiceImpl implements OrderAddressFindService {


    @Resource
    private orderMapper orderMapper;

    /**
     *
     * @Description: 获取不同状态订单
     *
     * @auther: 张松
     * @date: 11:17 2020/8/5
     * @param: [orderState] 订单状态的值
     * @return: java.util.List<cn.itcast.pojo.order>
     *
     */
    @Override
    public List<order> findOrderAddress(String orderState) {
        List<order> orderAddress = orderMapper.findOrderAddress(orderState);
        setOrderLatAndLon(orderAddress);
        return orderAddress;
    }

    /**
     *
     * @Description: 给不同状态订单设置经纬度
     *
     * @auther: 张松
     * @date: 11:18 2020/8/5
     * @param: [orderAddress] 订单
     * @return: void
     *
     */
    private void setOrderLatAndLon(List<order> orderAddress) {
        orderAddress.forEach((v)->{
            if (StringUtils.isBlank(v.getLat())||StringUtils.isBlank(v.getLon())){
                Map<String, Double> orderDetails = getOrderDetails(v);
                v.setLat(String.valueOf(orderDetails.get("lat")));
                v.setLon(String.valueOf(orderDetails.get("lng")));
            }
        });
    }

    /**
     *
     * @Description: 根据地址获取经纬度
     *
     * @auther: 张松
     * @date: 16:11 2020/8/5
     * @param: [v]
     * @return: cn.itcast.pojo.order
     *
     */
    private Map<String, Double> getOrderDetails(order orderOne) {
        Map<String, Double> lngAndLat = MyUtil.getLngAndLat(orderOne.getUserArea());
        if (lngAndLat.size()!=0||!lngAndLat.isEmpty()){
            return lngAndLat;
        }
        return null;

    }
}
